<?php

namespace App\Http\Controllers\Manage;

use App\Http\Model\Manage\AdminRuleModel;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class HomeController extends BasicController
{
    /**
     * 首页
     * @param Request $request
     * @return View
     */
    public function homePageView(Request $request): View
    {
        $admin = $request->session()->get("manage_admin");
        $role = $request->session()->get("manage_role");
        $roleRule = Cache::rememberForever("role_rule_".$role->code, function () use ($role) {
            if ($role->ltarCode == "-1")
            {
                $rules = AdminRuleModel::where([
                    ['status', '=', 1],
                    ['deleted', '=', 0]
                ])->orderBy("sort", "asc")->get()->toArray();
            }else{
                $ruleCodes = explode(",", $role->ltarCode);
                $rules = AdminRuleModel::whereIn("code", $ruleCodes)->where([
                    ['status', '=', 1],
                    ['deleted', '=', 0]
                ])->orderBy("sort", "asc")->get()->toArray();
            }

            if (!isset($rules) || empty($rules)) {
                return [];
            }
            return dataToTree($rules, "code", "pCode");
        });
        return view($this->viewPath."home",["admin"=>$admin, "role"=>$role, "rules"=>$roleRule]);
    }

    /**
     * 欢迎页面
     * @param Request $request
     * @return View
     */
    public function welcomePageView(Request $request): View
    {
        return view($this->viewPath."welcome");
    }


    /**
     * 清除缓存
     * @return array|string
     */
    public function clearAllCache(): array|string
    {
        Cache::flush();
        return successReturn("清除缓存成功");
    }
}
